﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class WindowBusinessSummaryController : Controller
    {
        private WindowBusinessSummaryInfo funinfo = new WindowBusinessSummaryInfo();
        public ActionResult WindowBusinessSummaryIndex()
        {
            ViewData["Node"] = GetNode();
            DataSet ds = funinfo.GetPeriod();
            WindowBusinessSummaryVM vm = new WindowBusinessSummaryVM();
            vm.StartTime = DateTime.Now.ToString("yyyy-MM-dd") + " " + ds.Tables[0].Rows[0][1].ToString()+":00";
            if (ds.Tables[0].Rows[1][2].ToString() == "True")
            {
                vm.EndTime = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") + " " + ds.Tables[0].Rows[1][1].ToString() + ":00";
            }
            else
            {
                vm.EndTime = DateTime.Now.ToString("yyyy-MM-dd") + " " + ds.Tables[0].Rows[1][1].ToString() + ":00";
            }
            return View(vm);
        }

        /// <summary>
        /// 获取消费组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetNode()
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            DataSet ds = funinfo.GetNode();
            dic.Add("ALL", "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(ds.Tables[0].Rows[i]["nodeid"].ToString().Trim(), ds.Tables[0].Rows[i]["nodename"].ToString().Trim());
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetList(PageVM pvm)
        {
            string condition = pvm.query;
            string strst = condition.Split(',')[0].ToString();
            string stret = condition.Split(',')[1].ToString();
            string strnodeid = condition.Split(',')[2].ToString();
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            DataSet list = funinfo.GetList(strst, stret, strnodeid);
            jsonFlexiGridData.total = list.Tables[0].Rows.Count;
            List<string> v;
            int i = 1;
            foreach (DataRow item in list.Tables[0].Rows)
            {
                v = new List<string>();
                v.Add(i.ToString());
                v.Add(item["MAC"].ToString());
                v.Add(item["COUNT1"].ToString());
                v.Add(item["Sum1"].ToString());
                v.Add(item["COUNT2"].ToString());
                v.Add(item["Sum2"].ToString());
                v.Add(item["COUNT3"].ToString());
                v.Add(item["Sum3"].ToString());
                v.Add(item["COUNT4"].ToString());
                v.Add(item["Sum4"].ToString());
                v.Add(item["COUNT_ALL"].ToString());
                v.Add(item["Sum_ALL"].ToString());
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = i.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
                i++;
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 窗口营业汇总报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string st, string et, string nodeid, string nodename)
        {
            return EContent(st, et, nodeid, nodename);
        }

        public ExcelContentResult EContent(string st, string et, string nodeid, string nodename)
        {
            DataSet list = funinfo.GetList(st, et, nodeid);
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='11' style='font-size:20px; text-align:center;'>上海港集装箱公司外高桥码头公司食堂餐饮管理信息系统</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='11' style='font-size:25px; text-align:center;'>窗口营业汇总表</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='11' style='font-size:17px; text-align:left;'>&nbsp;&nbsp;统计日期：从" + Convert.ToDateTime(st).ToString("yyyy-MM-dd") +
                "到" + Convert.ToDateTime(et).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;统计范围：" + nodename + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td rowspan='2' style='text-align:center;'>窗口</td>");
            sBuilder.Append("<td colspan='2' style='text-align:center;'>早餐</td>");
            sBuilder.Append("<td colspan='2' style='text-align:center;'>中餐</td>");
            sBuilder.Append("<td colspan='2' style='text-align:center;'>晚餐</td>");
            sBuilder.Append("<td colspan='2' style='text-align:center;'>夜餐</td>");
            sBuilder.Append("<td colspan='2' style='text-align:center;'>合计</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px;white-space: nowrap;'>");
            sBuilder.Append("<td style='text-align:center;'>人次</td>");
            sBuilder.Append("<td style='text-align:center;'>金额</td>");
            sBuilder.Append("<td style='text-align:center;'>人次</td>");
            sBuilder.Append("<td style='text-align:center;'>金额</td>");
            sBuilder.Append("<td style='text-align:center;'>人次</td>");
            sBuilder.Append("<td style='text-align:center;'>金额</td>");
            sBuilder.Append("<td style='text-align:center;'>人次</td>");
            sBuilder.Append("<td style='text-align:center;'>金额</td>");
            sBuilder.Append("<td style='text-align:center;'>人次</td>");
            sBuilder.Append("<td style='text-align:center;'>金额</td>");
            sBuilder.Append("</tr>");

            for (int i = 0; i < list.Tables[0].Rows.Count; i++)
            {
                if (i == list.Tables[0].Rows.Count - 1)
                {
                    sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                    sBuilder.Append("<td colspan='11'></td>");
                    sBuilder.Append("</tr>");
                }
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                sBuilder.Append("<td style='text-align:center;'>" + list.Tables[0].Rows[i]["MAC"].ToString() + "</td>");
                sBuilder.Append("<td>" + list.Tables[0].Rows[i]["COUNT1"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[i]["SUM1"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + list.Tables[0].Rows[i]["COUNT2"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[i]["SUM2"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + list.Tables[0].Rows[i]["COUNT3"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[i]["SUM3"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + list.Tables[0].Rows[i]["COUNT4"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[i]["SUM4"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("<td>" + list.Tables[0].Rows[i]["COUNT_ALL"].ToString() + "</td>");
                sBuilder.Append("<td>" + Math.Round(Convert.ToDecimal(list.Tables[0].Rows[i]["Sum_ALL"]), 2, MidpointRounding.AwayFromZero) + "</td>");
                sBuilder.Append("</tr>");
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td colspan='11' style='font-size:14px; text-align:left;'>&nbsp;&nbsp;打印日期：" + DateTime.Now.ToString("yyyy/MM/dd") + "&nbsp;&nbsp;&nbsp;" +
                "打印时间：" + DateTime.Now.ToString("HH:mm:ss") + "&nbsp;&nbsp;&nbsp;制表人：" +
                ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName + "</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "chuangkouhuizong";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
